package lc.com.lcpicturebackend.controller;

import cn.hutool.core.util.ObjUtil;
import lc.com.lcpicturebackend.common.BaseResponse;
import lc.com.lcpicturebackend.common.ResultUtils;
import lc.com.lcpicturebackend.exception.ErrorCode;
import lc.com.lcpicturebackend.exception.ThrowUtils;
import lc.com.lcpicturebackend.model.dto.space.analyze.*;
import lc.com.lcpicturebackend.model.entity.Space;
import lc.com.lcpicturebackend.model.entity.User;
import lc.com.lcpicturebackend.model.vo.analyze.*;
import lc.com.lcpicturebackend.service.SpaceAnalyzeService;
import lc.com.lcpicturebackend.service.SpaceService;
import lc.com.lcpicturebackend.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 功能：空间分析成控制器
 * 作者：lc
 * 日期：2024/12/22 14:09
 */
@Slf4j
@RestController
@RequestMapping("/spaceAnalyze")
public class SpaceAnalyzeController {
    @Resource
    private UserService userService;

    @Resource
    private SpaceService spaceService;
    @Resource
    private SpaceAnalyzeService spaceAnalyzeService;


    /**
     * 获取空间使用分析数据
     *
     * @return 返回空间使用分析数据
     */
    @PostMapping("/usage")
    public BaseResponse<SpaceUsageAnalyzeResponse> getSpaceUsageAnalyze(
            @RequestBody SpaceUsageAnalyzeRequest spaceUsageAnalyzeRequest,
            HttpServletRequest request) {
        ThrowUtils.throwIf(ObjUtil.isEmpty(spaceUsageAnalyzeRequest), ErrorCode.PARAMS_ERROR);
        User loginUser = userService.getLoginUser(request);
        SpaceUsageAnalyzeResponse spaceUsageAnalyze = spaceAnalyzeService
                .getSpaceUsageAnalyze(spaceUsageAnalyzeRequest, loginUser);
        return ResultUtils.success(spaceUsageAnalyze);

    }

    /**
     * 获取空间分类分析数据
     */
    @PostMapping("/category")
    public BaseResponse<List<SpaceCategoryAnalyzeResponse>> getSpaceCategoryAnalyze(
            @RequestBody SpaceCategoryAnalyzeRequest categoryAnalyzeRequest,
            HttpServletRequest request) {
        ThrowUtils.throwIf(ObjUtil.isEmpty(categoryAnalyzeRequest), ErrorCode.PARAMS_ERROR);
        User loginUser = userService.getLoginUser(request);
        List<SpaceCategoryAnalyzeResponse> spaceCategoryAnalyze = spaceAnalyzeService
                .getSpaceCategoryAnalyze(categoryAnalyzeRequest, loginUser);
        return ResultUtils.success(spaceCategoryAnalyze);
    }

    /**
     * 获取空间资源标签分析数据
     */
    @PostMapping("/tagAnalyze")
    public BaseResponse<List<SpaceTagAnalyzeResponse>> getSpaceResourceTagAnalyze(
            @RequestBody SpaceTagAnalyzeRequest tagAnalyzeRequest,
            HttpServletRequest request) {
        ThrowUtils.throwIf(ObjUtil.isEmpty(tagAnalyzeRequest), ErrorCode.PARAMS_ERROR);
        User loginUser = userService.getLoginUser(request);
        List<SpaceTagAnalyzeResponse> spaceTagAnalyze = spaceAnalyzeService
                .getSpaceResourceTagAnalyze(tagAnalyzeRequest, loginUser);
        return ResultUtils.success(spaceTagAnalyze);
    }

    /**
     * 获取空间资源图片大小分析数据
     */
    @PostMapping("/sizeAnalyze")
    public BaseResponse<List<SpaceSizeAnalyzeResponse>> getSpaceResourceSizeAnalyze(
            @RequestBody SpaceSizeAnalyzeRequest sizeAnalyzeRequest,
            HttpServletRequest request) {
        ThrowUtils.throwIf(ObjUtil.isEmpty(sizeAnalyzeRequest), ErrorCode.PARAMS_ERROR);
        User loginUser = userService.getLoginUser(request);
        List<SpaceSizeAnalyzeResponse> spaceSizeAnalyze = spaceAnalyzeService
                .getSpaceResourceSizeAnalyze(sizeAnalyzeRequest, loginUser);
        return ResultUtils.success(spaceSizeAnalyze);
    }

    /**
     * 获取用户上传分析数据
     */
    @PostMapping("/userLoadAnalyze")
    public BaseResponse<List<SpaceUserAnalyzeResponse>> getSpaceUserAnalyze(
            @RequestBody SpaceUserAnalyzeRequest userAnalyzeRequest,
            HttpServletRequest request) {
        ThrowUtils.throwIf(ObjUtil.isEmpty(userAnalyzeRequest), ErrorCode.PARAMS_ERROR);
        User loginUser = userService.getLoginUser(request);
        List<SpaceUserAnalyzeResponse> spaceUserAnalyze = spaceAnalyzeService
                .getSpaceUserAnalyze(userAnalyzeRequest, loginUser);
        return ResultUtils.success(spaceUserAnalyze);
    }

    /**
     * 获取空间资源大小分析数据
     */
    @PostMapping("/rank")
    public BaseResponse<List<Space>> getSpaceRankAnalyze(@RequestBody SpaceRankAnalyzeRequest spaceRankAnalyzeRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(spaceRankAnalyzeRequest == null, ErrorCode.PARAMS_ERROR);
        User loginUser = userService.getLoginUser(request);
        List<Space> resultList = spaceAnalyzeService.getSpaceRankAnalyze(spaceRankAnalyzeRequest, loginUser);
        return ResultUtils.success(resultList);
    }


}